package cn.edu.cdtu.healthserver.servlet;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import cn.edu.cdtu.healthserver.dao.ChooseLogDao;
import cn.edu.cdtu.healthserver.dao.TestLogDao;
import cn.edu.cdtu.healthserver.dao.impl.ChooseLogDaoImplementation;
import cn.edu.cdtu.healthserver.dao.impl.TestLogDaoImplementation;
import cn.edu.cdtu.healthserver.domain.ChooseLog;
import cn.edu.cdtu.healthserver.domain.Test;
import cn.edu.cdtu.healthserver.domain.TestLog;
import cn.edu.cdtu.healthserver.domain.UserInformation;
import cn.edu.cdtu.healthserver.service.LoginControl;
import cn.edu.cdtu.healthserver.utils.ConnectUtil;

/**
 * Servlet implementation class TestLogServletForClient
 */
@WebServlet("/TestLogServletForClient")
public class TestLogServletForClient extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private TestLogDao testLogDao = new TestLogDaoImplementation();
	private ChooseLogDao chooseLogDao = new ChooseLogDaoImplementation();
	private Gson gson = new Gson();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestLogServletForClient() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String option = request.getParameter(
				ConnectUtil.HealthTestPargrams.Option.HEALTH_TEST_OPTION);
		if(ConnectUtil.HealthTestPargrams.Option
				.OptionValue.HEALTH_TEST_OPTION_GET_TEST_LOG.equals(option)){
			getTestLog(request, response);
		}else if (ConnectUtil.HealthTestPargrams.Option
				.OptionValue.HEALTH_TEST_OPTION_GET_QUESTION_LOG.equals(option)) {
			getChooseLog(request, response);
		}
	}
	/**
	 * 
	 * @Title: getTestLog 
	 * @Description: TODO
	 * @return void
	 * @throws 
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getTestLog(HttpServletRequest request, HttpServletResponse response)
			throws IOException{
		UserInformation loginUser = LoginControl.getLoginedUser(request);
		OutputStream writer = response.getOutputStream();
		if(loginUser != null){
			String testJson = request.getParameter(
					ConnectUtil.HealthTestPargrams.Pargram.SELECT_TEST);
			Test test = gson.fromJson(testJson, Test.class);
			List<TestLog> testLogs = testLogDao.query(loginUser, test);
			response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
			writer.write(gson.toJson(testLogs).getBytes());
		}else{
			writer.write(ConnectUtil.HealthTestPargrams
					.Result.NO_LOGIN_USER.getBytes());
		}
		writer.flush();
		writer.close();
	}
	/**
	 * 
	 * @Title: getQuestionLog 
	 * @Description: TODO
	 * @return void
	 * @throws 
	 *
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getChooseLog(HttpServletRequest request, HttpServletResponse response)
			throws IOException{
		String logJson = request.getParameter(
				ConnectUtil.HealthTestPargrams.Pargram.SELECT_TEST_LOG);
		OutputStream writer = response.getOutputStream();
		if(logJson != null){
			TestLog testLog = gson.fromJson(logJson, TestLog.class);
			List<ChooseLog> chooseLogs = chooseLogDao.query(testLog);
			response.setContentType(ConnectUtil.ContentTypeString.jSON_STRING);
			writer.write(gson.toJson(chooseLogs).getBytes());
		}else{
			writer.write(ConnectUtil.HealthTestPargrams
					.Result.NO_DATA.getBytes());
		}
		writer.flush();
		writer.close();
	}

}
